<%@ page import="java.sql.*" %>
<%@ page contentType="text/html;charset=UTF-8" %>
<!DOCTYPE html >
<html lang="en" onclick="window.location.reload()">
<head>
    <style>
        body {
            background: linear-gradient(to right, #fee0a2, #f98d92, #ce65af, #8e51d3);
        }

        table {
            width: 100%;
            border-collapse: collapse;
        }

        table, th, td {
            border: 1px solid black;
        }

        th, td {
            padding: 8px;
            text-align: left;
        }
        button {
            padding: 5px 10px;
            background-color: #0074cc;
            color: white;
            border: none;
            cursor: pointer;
        }

        button:hover {
            background-color: #0056a7;
        }
    </style>
    <title>管理员-会员界面</title>
</head>

<body onclick="window.location.reload()">

<h1 style="display: flex; justify-content: left;align-items:unset;">会员列表</h1>
<button id="addUserBtn" onclick="openAddUserWindow()" style="font-size: 20px"><b>添加会员</b></button>
<table id="UserTable">
    <tr>
        <th>ID</th>
        <th>姓名</th>
        <th>手机号</th>
        <th>密码</th>
        <th>操作</th>
    </tr>
    <!-- 使用JSP动态生成会员列表 -->
    <%@ page import="java.sql.*" %>
    <%
        Connection conn = null;
        ResultSet rs = null;
        Statement stmt = null;

        try {
            Class.forName("com.mysql.jdbc.Driver");
            conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/course_design", "root", "123456");
            stmt = conn.createStatement();
            String sql = "SELECT * FROM users";
            rs = stmt.executeQuery(sql);
            while (rs.next()) {
    %>
    <tr>
        <td><%= rs.getInt("uid") %>
        </td>
        <td><%= rs.getString("name") %>
        </td>
        <td><%= rs.getString("phone_number") %>
        </td>
        <td><%= rs.getString("password") %>
        </td>
        <td style="display: flex;padding: 10px;gap: 100px">
            <button onclick="openEditUserWindow(<%= rs.getInt("uid") %>, '<%= rs.getString("name") %>', '<%= rs.getString("phone_number") %>', '<%= rs.getString("password") %>')">
                编辑
            </button>
            <form action="deleteUser" method="post">
                <input type="hidden" name="userId" value="<%= rs.getInt("uid") %>">
                <button onclick="confirmDelete(<%= rs.getInt("uid") %>)" style="background: red">删除</button>
            </form>
        </td>
    </tr>
    <%
            }
        } catch (Exception e) {
            e.printStackTrace(System.err);
        } finally {
            try {
                if (rs != null) rs.close();
            } catch (Exception ignored) {
            }
            ;
            try {
                if (stmt != null) stmt.close();
            } catch (Exception ignored) {
            }
            ;
            try {
                if (conn != null) conn.close();
            } catch (Exception ignored) {
            }
            ;
        }
    %>
</table>

<script>
    function openAddUserWindow() {
        window.open("AddUser.jsp", "_blank", "width=550, height=470");
    }

    function openEditUserWindow(id, name, phoneNumber, password) {
        const editUrl = "editUser.jsp?id=" + id + "&name=" + name + "&phoneNumber=" + phoneNumber + "&password=" + password;
        window.open(editUrl, "_blank", "width=550,height=470");
    }

    function confirmDelete(userId) {
        const confirmDelete = confirm("确认删除用户"+userId+"吗？");

        if (confirmDelete) {
            deleteAccount(userId);
        } else {
        }
    }

    function deleteAccount(userId) {
        // 构建一个表单，用于提交删除操作
        const form = document.createElement("form");
        form.setAttribute("method", "post");
        form.setAttribute("action", "deleteUser");

        // 创建一个隐藏的 input 元素，用于传递用户ID
        const input = document.createElement("input");
        input.setAttribute("type", "hidden");
        input.setAttribute("value", userId);

        // 将 input 元素添加到 form 中
        form.appendChild(input);

        // 将 form 元素添加到页面中
        document.body.appendChild(form);

        // 提交表单，执行删除操作
        form.submit();
        window.location.href = "Admin_User.jsp"
    }
</script>

</body>
</html>